/*******************************************************************************

  Paying Outsourced Labor: Direct Evidence from Linked Temp Agency-Worker-Client Data

  By Andres Drenik, Simon Jäger, Pascuel Plotkin and Benjamin Schoefer
  January 7th, 2021

	DESCRIPTION: Creates Figures 4

*******************************************************************************/




/********************************************************************************
***** Preliminaries
********************************************************************************/
set more off
cap log close
local curr_date = c(current_date)
log using "${logs}/Fig4`curr_date'", replace


/****************************************************************************************
* Load Dataset
****************************************************************************************/

use "${intermediate_data_emp}/AKM_Firm_FE_for_export_stata.dta", clear

/****************************************************************************************
* Run Figures
****************************************************************************************/

    keep if uf_fe4 != . & uf_fe4!= 0 & firm_fe4 != . & firm_fe4 != 0 & connected_set == 1
  	rename obs total_obs

	** Figure 4.A

	*Winsorize (trim) Firm Fixed Effects
    winsor2 firm_fe4, replace cuts(1 99) trim
    winsor2 uf_fe4, replace cuts(1 99) trim

    *Calculate Leave-One-Out Tenure
    gen tenure_weight = tenure*total_obs
    bys ciiu_4_user: egen sum_tenure = sum(tenure_weight)
    bys ciiu_4_user: egen total_obs_industry = sum(total_obs)
    gen lom_tenure = (sum_tenure - tenure_weight)/(total_obs_industry - total_obs)

    *Splitting industries by Regular (Leave one out) Tenure Median
    sum lom_tenure if uf_fe4 != . & uf_fe4!= 0 & firm_fe4 != . & firm_fe4 != 0 & connected_set == 1 [aw=total_obs], detail
    cap drop sample
    gen sample = (lom_tenure<r(p50)) & uf_fe4 != . & uf_fe4!= 0 & firm_fe4 != . & firm_fe4 != 0 & connected_set == 1

  	*Mean of each group
  	qui sum lom_tenure [aw=total_obs] if sample == 1
  	global tenure_1 = r(mean)
  	display("The average tenure for the group below the median is: ${tenure_1}")

  	qui sum lom_tenure [aw=total_obs] if sample == 0
  	global tenure_0 = r(mean)
  	display("The average tenure for the group above the median is: ${tenure_0}")

    *<p50
    binscatter uf_fe4 firm_fe4 [aw=total_obs] if sample==1,  savedata(sample1_t) replace ytitle("Firm FE for Temp Workers", size(medium)) xtitle("Firm FE for Regular Workers", size(medium)) scheme(vg_outc) legend(off) graphregion(color(white)) plotregion(color(white)) xsize(16) ysize(9) n(20)
    reg uf_fe4 firm_fe4 [aw=total_obs] if sample==1
    local beta_firm_fe4_s1_t = _b[firm_fe4]
    local se_firm_fe4_s1_t = _se[firm_fe4]
    local constant_s1_t = _b[_cons]

    *>p50
    binscatter uf_fe4 firm_fe4 [aw=total_obs] if sample==0,  savedata(sample0_t) replace ytitle("Firm FE for Temp Workers", size(medium)) xtitle("Firm FE for Regular Workers", size(medium)) scheme(vg_outc) legend(off) graphregion(color(white)) plotregion(color(white)) xsize(16) ysize(9) n(20)
    reg uf_fe4 firm_fe4 [aw=total_obs] if sample==0
    local beta_firm_fe4_s0_t = _b[firm_fe4]
    local se_firm_fe4_s0_t = _se[firm_fe4]
    local constant_s0_t = _b[_cons]

    *<p25
    cap drop sample
    sum lom_tenure if uf_fe4 != . & uf_fe4!= 0 & firm_fe4 != . & firm_fe4 != 0 & connected_set == 1 [aw=total_obs], detail
    gen sample = (lom_tenure<r(p25)) & uf_fe4 != . & uf_fe4!= 0 & firm_fe4 != . & firm_fe4 != 0 & connected_set == 1
    reg uf_fe4 firm_fe4 [aw=total_obs] if sample==1
    binscatter uf_fe4 firm_fe4 [aw=total_obs] if sample==1

    preserve

    clear
    qui do sample0_t
    gen sample_n = 0

    save "${intermediate_data_emp}\sample0_t", replace

    restore

    preserve

    clear
    qui do sample1_t
    gen sample_n = 1
    append using "${intermediate_data_emp}\sample0_t.dta"

    *Normalization
    bys sample_n (firm_fe4): gen order = _n
    sum firm_fe4 if sample_n == 0 & order == 1
    local mean_reg_s0_t = r(mean)
    replace firm_fe4 = firm_fe4 - `mean_reg_s0_t' if sample_n == 0

    sum firm_fe4 if sample_n == 1 & order == 1
    local mean_reg_s1_t = r(mean)
    replace firm_fe4 = firm_fe4 - `mean_reg_s1_t' if sample_n == 1

    local norm_var_s0_t = `constant_s0_t' + `beta_firm_fe4_s0_t' * `mean_reg_s0_t'
    local norm_var_s1_t = `constant_s1_t' + `beta_firm_fe4_s1_t' * `mean_reg_s1_t'

    replace uf_fe4 = uf_fe4 - `norm_var_s0_t' if sample_n == 0
    replace uf_fe4 = uf_fe4 - `norm_var_s1_t' if sample_n == 1

    *Plot Figure
    twoway scatter uf_fe4 firm_fe4 if sample_n == 0, mcolor(gs4) msize(medlarge) || scatter uf_fe4 firm_fe4 if sample_n == 1, mcolor(gs7) msymbol(Th) msize(medlarge) || function y= `beta_firm_fe4_s0_t'*x , range(0 1) color(gs4) || function y= `beta_firm_fe4_s1_t'*x , range(0 1) color(gs7) lpattern(dash)  /*
    */ ytitle("Firm FE for Temp Workers") xtitle("Firm FE for Regular Workers") scheme(vg_outc) legend(off) graphregion(color(white)) plotregion(color(white)) xsc(r(0 1)) ysc(r(0 1)) ylab(0(.2)1 ,nogrid) xlabel(0(.2)1) xsize(16) ysize(9) text(.65 .87 "Above Median" "Tenure" "{&gamma} = 0.54 (SE 0.0129)", color(gs4)) text(.27 .9 "Below Median" "Tenure" "{&gamma} = 0.45 (SE 0.0110)", color(gs7))
    graph export "${Results}/ScatterPlot_akm4_firmFE_addedSlope_Tenure.pdf", replace
    restore

    ** Figure 4.B

    *Generate Leave-One-Out Firm Fixed Effects
    gen firm_fe4_weight = firm_fe4*total_obs
    bys ciiu_4_user: egen sum_firm_fe4 = sum(firm_fe4_weight)
    gen lom_firm_fe4 = (sum_firm_fe4-firm_fe4_weight)/(total_obs_industry-total_obs)
		
    *Generate Leave-One-Out Standard Deviation
    gen x_square_weighted = (firm_fe4^2)*total_obs
    bys ciiu_4_user: egen x_square_ind = sum(x_square_weighted)
    gen lom_x_square_ind =  (x_square_ind - x_square_weighted)/(total_obs_industry-total_obs)
    gen lom_x_bar_square = lom_firm_fe4^2
    gen lom_sd_firm_fe4 = sqrt(lom_x_square_ind - lom_x_bar_square)

    *Spliting by Regular Firm FE SD
    sum lom_sd_firm_fe4 if uf_fe4 != . & uf_fe4!= 0 & firm_fe4 != . & firm_fe4 != 0 & connected_set == 1 [aw=total_obs], detail
    cap drop sample
    gen sample = (lom_sd_firm_fe4<r(p50)) & uf_fe4 != . & uf_fe4!= 0 & firm_fe4 != . & firm_fe4 != 0 & connected_set == 1

  	*Mean of each group
  	qui sum lom_sd_firm_fe4 [aw=total_obs] if sample == 1
  	global sd_1 = r(mean)
  	display("The average SD for the group below the median is: ${sd_1}")

  	qui sum lom_sd_firm_fe4 [aw=total_obs] if sample == 0
  	global sd_0 = r(mean)
  	display("The average SD for the group above the median is: ${sd_0}")

    *<p50
    binscatter uf_fe4 firm_fe4 [aw=total_obs] if sample==1,  savedata(sample1_sd) replace ytitle("Firm FE for Temp Workers", size(medium)) xtitle("Firm FE for Regular Workers", size(medium)) scheme(vg_outc) legend(off) graphregion(color(white)) plotregion(color(white)) xsize(16) ysize(9) n(20)
    reg uf_fe4 firm_fe4 [aw=total_obs] if sample==1
    local beta_firm_fe4_s1_sd = _b[firm_fe4]
    local se_firm_fe4_s1_sd = _se[firm_fe4]
    local constant_s1_sd = _b[_cons]

    *>p50
    binscatter uf_fe4 firm_fe4 [aw=total_obs] if sample==0,  savedata(sample0_sd) replace ytitle("Firm FE for Temp Workers", size(medium)) xtitle("Firm FE for Regular Workers", size(medium)) scheme(vg_outc) legend(off) graphregion(color(white)) plotregion(color(white)) xsize(16) ysize(9) n(20)
    reg uf_fe4 firm_fe4 [aw=total_obs] if sample==0
    local beta_firm_fe4_s0_sd = _b[firm_fe4]
    local se_firm_fe4_s0_sd = _se[firm_fe4]
    local constant_s0_sd = _b[_cons]

    preserve

    clear
    qui do sample0_sd
    gen sample_n = 0

    save "${intermediate_data_emp}\sample0_sd", replace

    restore

    preserve

    clear

    qui do sample1_sd
    gen sample_n = 1
    append using "${intermediate_data_emp}\sample0_sd.dta"

    *Normalization
    bys sample_n (firm_fe4): gen order = _n
    sum firm_fe4 if sample_n == 0 & order == 1
    local mean_reg_s0_sd = r(mean)
    replace firm_fe4 = firm_fe4 - `mean_reg_s0_sd' if sample_n == 0

    sum firm_fe4 if sample_n == 1 & order == 1
    local mean_reg_s1_sd = r(mean)
    replace firm_fe4 = firm_fe4 - `mean_reg_s1_sd' if sample_n == 1

    local norm_var_s0_sd = `constant_s0_sd' + `beta_firm_fe4_s0_sd' * `mean_reg_s0_sd'
    local norm_var_s1_sd = `constant_s1_sd' + `beta_firm_fe4_s1_sd' * `mean_reg_s1_sd'

    replace uf_fe4 = uf_fe4 - `norm_var_s0_sd' if sample_n == 0
    replace uf_fe4 = uf_fe4 - `norm_var_s1_sd' if sample_n == 1

    *Plot Figure
    twoway scatter uf_fe4 firm_fe4 if sample_n == 0, mcolor(gs4) msize(medlarge) || scatter uf_fe4 firm_fe4 if sample_n == 1, mcolor(gs7) msymbol(Th) msize(medlarge) || function y= `beta_firm_fe4_s0_sd'*x , range(0 1) color(gs4) || function y= `beta_firm_fe4_s1_sd'*x , range(0 1) color(gs7) lpattern(dash)  /*
    */ ytitle("Firm FE for Temp Workers") xtitle("Firm FE for Regular Workers") scheme(vg_outc) legend(off) graphregion(color(white)) plotregion(color(white)) xsc(r(0 1)) ysc(r(0 1)) ylab(0(.2)1 ,nogrid) xlabel(0(.2)1) xsize(16) ysize(9) text(.65 .75 "Above Median" "SD of AKM Pay Premia" "{&gamma} = 0.53 (SE 0.0085)", color(gs4)) text(.12 .75 "Below Median"  "SD of AKM Pay Premia" "{&gamma} = 0.48 (SE 0.0155)", color(gs7))
    graph export "${Results}/ScatterPlot_akm4_firmFE_addedSlope_IndustrySDSamples_weighted.pdf", replace
    restore

    ** Figure 4.C

    *Spliting by Regular Firm FE
    sum lom_firm_fe4 if uf_fe4 != . & uf_fe4!= 0 & firm_fe4 != . & firm_fe4 != 0 & connected_set == 1 [aw=total_obs], detail
    cap drop sample
    gen sample = (lom_firm_fe4<r(p50)) & uf_fe4 != . & uf_fe4!= 0 & firm_fe4 != . & firm_fe4 != 0 & connected_set == 1

  	*Mean of each group
  	qui sum lom_firm_fe4 [aw=total_obs] if sample == 1
  	global firm_fe4_1 = r(mean)
  	display("The average Firm FE for the group below the median is: ${firm_fe4_1}")

  	qui sum lom_firm_fe4 [aw=total_obs] if sample == 0
  	global firm_fe4_0 = r(mean)
  	display("The average Firm FE for the group above the median is: ${firm_fe4_0}")

    *<p50
    binscatter uf_fe4 firm_fe4 [aw=total_obs] if sample==1,  savedata(sample1_fe) replace ytitle("Firm FE for Temp Workers", size(medium)) xtitle("Firm FE for Regular Workers", size(medium)) scheme(vg_outc) legend(off) graphregion(color(white)) plotregion(color(white)) xsize(16) ysize(9) n(20)
    reg uf_fe4 firm_fe4 [aw=total_obs] if sample==1
    local beta_firm_fe4_s1_fe = _b[firm_fe4]
    local se_firm_fe4_s1_fe = _se[firm_fe4]
    local constant_s1_fe = _b[_cons]

    *>p50
    binscatter uf_fe4 firm_fe4 [aw=total_obs] if sample==0,  savedata(sample0_fe) replace ytitle("Firm FE for Temp Workers", size(medium)) xtitle("Firm FE for Regular Workers", size(medium)) scheme(vg_outc) legend(off) graphregion(color(white)) plotregion(color(white)) xsize(16) ysize(9) n(20)
    reg uf_fe4 firm_fe4 [aw=total_obs] if sample==0
    local beta_firm_fe4_s0_fe = _b[firm_fe4]
    local se_firm_fe4_s0_fe = _se[firm_fe4]
    local constant_s0_fe = _b[_cons]

    preserve

    clear
    qui do sample0_fe
    gen sample_n = 0

    save "${intermediate_data_emp}\sample0_fe", replace

    restore

    preserve

    clear

    qui do sample1_fe
    gen sample_n = 1
    append using "${intermediate_data_emp}\sample0_fe.dta"

    *Normalization
    bys sample_n (firm_fe4): gen order = _n
    sum firm_fe4 if sample_n == 0 & order == 1
    local mean_reg_s0_fe = r(mean)
    replace firm_fe4 = firm_fe4 - `mean_reg_s0_fe' if sample_n == 0

    sum firm_fe4 if sample_n == 1 & order == 1
    local mean_reg_s1_fe = r(mean)
    replace firm_fe4 = firm_fe4 - `mean_reg_s1_fe' if sample_n == 1

    local norm_var_s0_fe = `constant_s0_fe' + `beta_firm_fe4_s0_fe' * `mean_reg_s0_fe'
    local norm_var_s1_fe = `constant_s1_fe' + `beta_firm_fe4_s1_fe' * `mean_reg_s1_fe'

    replace uf_fe4 = uf_fe4 - `norm_var_s0_fe' if sample_n == 0
    replace uf_fe4 = uf_fe4 - `norm_var_s1_fe' if sample_n == 1

    *Plot Figure
    twoway scatter uf_fe4 firm_fe4 if sample_n == 0, mcolor(gs4) msize(medlarge) || scatter uf_fe4 firm_fe4 if sample_n == 1, mcolor(gs7) msymbol(Th) msize(medlarge) || function y= `beta_firm_fe4_s0_fe'*x , range(0 1) color(gs4) || function y= `beta_firm_fe4_s1_fe'*x , range(0 1) color(gs7) lpattern(dash)  /*
    */ ytitle("Firm FE for Temp Workers") xtitle("Firm FE for Regular Workers") scheme(vg_outc) legend(off) graphregion(color(white)) plotregion(color(white)) xsc(r(0 1)) ysc(r(0 1)) ylab(0(.2)1 ,nogrid) xlabel(0(.2)1) xsize(16) ysize(9) text(.62 .75 "Above Median" "AKM Pay Premia" "{&gamma} = 0.54 (SE 0.0145)", color(gs4)) text(.17 .75 "Below Median" "AKM Pay Premia" "{&gamma} = 0.46 (SE 0.0105)", color(gs7))
    graph export "${Results}/ScatterPlot_akm4_firmFE_addedSlope_SplitFE.pdf", replace
    restore

    ** Figure 4.D
    merge m:1 ciiu_4_user using "${intermediate_data_emp}/cba_coverage_by_industry.dta", nogen

    sum share_cov if uf_fe4 != . & uf_fe4!= 0 & firm_fe4 != . & firm_fe4 != 0 & connected_set == 1 [aw=total_obs], detail
    cap drop sample
    gen sample = (share_cov<r(p50)) & uf_fe4 != . & uf_fe4!= 0 & firm_fe4 != . & firm_fe4 != 0 & connected_set == 1

	*Mean of each group
  	qui sum share_cov [aw=total_obs] if sample == 1
  	global share_cov_1 = r(mean)
  	display("The average coverage for the group below the median is: ${share_cov_1}")

  	qui sum share_cov [aw=total_obs] if sample == 0
  	global share_cov_0 = r(mean)
  	display("The average coverage for the group above the median is: ${share_cov_0}")

    *<p50
    binscatter uf_fe4 firm_fe4 [aw=total_obs] if sample==1,  savedata(sample1_cba) replace ytitle("Firm FE for Temp Workers", size(medium)) xtitle("Firm FE for Regular Workers", size(medium)) scheme(vg_outc) legend(off) graphregion(color(white)) plotregion(color(white)) xsize(16) ysize(9) n(20)
    reg uf_fe4 firm_fe4 [aw=total_obs] if sample==1
    local beta_firm_fe4_s1_cba = _b[firm_fe4]
    local se_firm_fe4_s1_cba = _se[firm_fe4]
    local constant_s1_cba = _b[_cons]

    *>p50
    binscatter uf_fe4 firm_fe4 [aw=total_obs] if sample==0,  savedata(sample0_cba) replace ytitle("Firm FE for Temp Workers", size(medium)) xtitle("Firm FE for Regular Workers", size(medium)) scheme(vg_outc) legend(off) graphregion(color(white)) plotregion(color(white)) xsize(16) ysize(9) n(20)
    reg uf_fe4 firm_fe4 [aw=total_obs] if sample==0
    local beta_firm_fe4_s0_cba = _b[firm_fe4]
    local se_firm_fe4_s0_cba = _se[firm_fe4]
    local constant_s0_cba = _b[_cons]

    preserve

    clear
    qui do sample0_cba
    gen sample_n = 0

    save "${intermediate_data_emp}\sample0_cba", replace

    restore

    preserve

    clear

    qui do sample1_cba
    gen sample_n = 1
    append using "${intermediate_data_emp}\sample0_cba.dta"

    *Normalization
    bys sample_n (firm_fe4): gen order = _n
    sum firm_fe4 if sample_n == 0 & order == 1
    local mean_reg_s0_cba = r(mean)
    replace firm_fe4 = firm_fe4 - `mean_reg_s0_cba' if sample_n == 0

    sum firm_fe4 if sample_n == 1 & order == 1
    local mean_reg_s1_cba = r(mean)
    replace firm_fe4 = firm_fe4 - `mean_reg_s1_cba' if sample_n == 1

    local norm_var_s0_cba = `constant_s0_cba' + `beta_firm_fe4_s0_cba' * `mean_reg_s0_cba'
    local norm_var_s1_cba = `constant_s1_cba' + `beta_firm_fe4_s1_cba' * `mean_reg_s1_cba'

    replace uf_fe4 = uf_fe4 - `norm_var_s0_cba' if sample_n == 0
    replace uf_fe4 = uf_fe4 - `norm_var_s1_cba' if sample_n == 1

    *Plot Figure
    twoway scatter uf_fe4 firm_fe4 if sample_n == 0, mcolor(gs4) msize(medlarge) || scatter uf_fe4 firm_fe4 if sample_n == 1, mcolor(gs7) msymbol(Th) msize(medlarge) || function y= `beta_firm_fe4_s0_cba'*x , range(0 1) color(gs4) || function y= `beta_firm_fe4_s1_cba'*x , range(0 1) color(gs7) lpattern(dash)  /*
    */ ytitle("Firm FE for Temp Workers") xtitle("Firm FE for Regular Workers") scheme(vg_outc) legend(off) graphregion(color(white)) plotregion(color(white)) xsc(r(0 1)) ysc(r(0 1)) ylab(0(.2)1 ,nogrid) xlabel(0(.2)1) xsize(16) ysize(9) text(.62 .75 "Above Median" "CBA Coverage" "{&gamma} = 0.50 (SE 0.0154)", color(gs4)) text(.17 .75 "Below Median" "CBA Coverage" "{&gamma} = 0.51 (SE 0.0088)", color(gs7))
    graph export "${Results}/ScatterPlot_akm4_firmFE_addedSlope_CBA.pdf", replace
    restore

log close
